import 'dart:io';

import 'package:flutter/material.dart';
import 'package:hugeicons/hugeicons.dart';
import 'package:provider/provider.dart';
import '../../base/base_state.dart';
import '../../resources.dart';

import 'vm/image_manager_model.dart';

/// created by hcl
/// on 2024/5/9
/// ImageManagerPage
/// desc
class ImageManagerPage extends StatefulWidget {
  const ImageManagerPage({super.key});

  @override
  State<StatefulWidget> createState() => _ImageManagerState();
}

class _ImageManagerState
    extends BaseState<ImageManagerModel, ImageManagerPage> {
  @override
  String get title => '图片管理';

  @override
  ImageManagerModel initModel() {
    return ImageManagerModel();
  }

  @override
  Widget bodyWidget(BuildContext context) {
    return Consumer<ImageManagerModel>(
        builder: (BuildContext context, model, Widget? child) {
      return Padding(
        padding: EdgeInsets.all(8.ss),
        child: Column(
          children: [
            _buildTop(),
            Expanded(
              child: _buildList(),
            ),
          ],
        ),
      );
    });
  }

  Widget _buildTop() {
    return Container();
  }

  Widget _buildList() {
    return GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            childAspectRatio: 1,
            crossAxisSpacing: 12.ss,
            mainAxisSpacing: 24.ss),
        shrinkWrap: true,
        physics: const BouncingScrollPhysics(),
        itemCount: model.list.length,
        itemBuilder: (context, index) {
          var itemModel = model.list[index];
          return InkWell(
            onTap: () {
              model.goImage(itemModel.path);
            },
            child: Container(
              decoration: BoxDecoration(
                  color: Colors.transparent,
                  borderRadius: BorderRadius.circular(8.ss)),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    width: (designWidth / 3 + 2).ss,
                    height: (designWidth / 3 + 2).ss,
                    clipBehavior: Clip.hardEdge,
                    decoration: BoxDecoration(
                        color: ColorUtils.app_color.withOpacity(0.8),
                        borderRadius: BorderRadius.circular(15.ss)),
                    child: Container(
                        width: (designWidth / 3).ss,
                        height: (designWidth / 3).ss,
                        clipBehavior: Clip.hardEdge,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(16.ss)),
                        child:
                            fileImage(itemModel.des ?? '', fit: BoxFit.contain)),
                  ),
                  SizedBox(height: 3.ss),
                  Text(itemModel.name,
                      style: normal312,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis)
                ],
              ),
            ),
          );
        });
  }
}
